<template>
  <div style="width: 80%; margin-right: 10%; margin-left: 10%; margin-top: 2%">
    <el-form label-width="120px">
      <el-form-item label="地点标题">
        <el-input v-model="location.locationName" placeholder="请填写标题" />
      </el-form-item>

      <el-form-item label="价格">
        <el-input-number
          :min="1"
          :max="2000"
          :step="1"
          v-model="location.price"
          controls-position="right"
          placeholder="请填写展示价格"
        />
      </el-form-item>

      <el-form-item label="地点图片">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="BASE_URL"
          class="avatar-uploader"
        >
          <img :src="imageUrl" style="width: 320px; height: 320px" />
        </el-upload>
      </el-form-item>

      <el-form-item label="开放时间">
        <el-select v-model="location.openTime" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="场地标签">
        <el-select v-model="location.lableId" placeholder="请选择">
          <el-option
            v-for="item in lableList"
            :key="item.lableId"
            :label="item.lableName"
            :value="item.lableId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          v-if="isSave"
          type="primary"
          @click="saveOrUpdateLocation"
          >保存</el-button
        >
        <el-button
          v-else
          type="primary"
          @click="saveOrUpdateLocation"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
// <!-- :action="BASE_API+'/eduoss/fileoss/uploadFile'" -->
<script>
import location from "@/api/location";
import lable from "@/api/label";

export default {
  data() {
    return {
      BASE_URL: process.env.VUE_APP_BASE_API + "/weddingoss/fileoss/uploadFile",
      isSave: true,
      location: {},
      lableList: [], 
      lable: {},
      lableName: "",
      url: "eduoss/fileoss/uploadFile",
      imageUrl:
        "https://img1.baidu.com/it/u=3686273500,1950045651&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1672419600&t=40dcde0e36cec07218f8f480555e7dcc",
      options: [{
        value: '周一',
        label: '周一'
      }, {
        value: '周二',
        label: '周二'
      }, {
        value: '周三',
        label: '周三'
      }, {
        value: '周四',
        label: '周四'
      }, {
        value: '周五',
        label: '周五'
      }, {
        value: '周六',
        label: '周六'
      }, {
        value: '周日',
        label: '周日'
      }],
    };
  },

  watch: {
    $route(to, from) {
      this.init()
    },
  },

  created() {
    this.init()
  },
  methods: {
    init() {
      if (this.$route.query && this.$route.query.id) {
        // this.id = this.$route.query.id
        this.location.locationId = this.$route.query.id
        this.isSave = true;
        location.getOneById(this.location.locationId).then((res) => {
          if (res.success) {
            this.location = res.data.location;
            this.lable = res.data.location.locationLable
            this.lableName = res.data.location.locationLable.lableName
            this.imageUrl = this.location.locationUrl;
          } else {
            this.$message({
              message: "系统异常,稍后再试",
              type: "failed",
            });
          }
        });
      } else {
        this.location.locationUrl = this.imageUrl
        this.isSave = false;
      }

      lable.getAllLable().then( res => {
        if ( res.success ) {
          this.lableList = res.data.lableList
        } else {
          this.$message({
            message: "Error",
            type: "failed",
          })
        }
      })
    },

    handleSelect(item) {
      this.location.openTime = item
    },

    querySearch(queryString, cb) {
      const week = [
        "周一","周二","周三","周四","周五","周六","周日",
      ]
      cb(week);
    },

    handleAvatarSuccess(res, file) {
      this.location.locationUrl = res.data.url
      this.imageUrl = res.data.url
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    saveOrUpdateLocation() {
      location.saveOrUpdate(this.location).then(res => {
        if ( res.success ) {
            if ( this.isSave ) {
                this.$message({
                  message: "保存成功",
                  type: "success",
                });
            } else {
                this.location = {}
                this.imageUrl = "https://img1.baidu.com/it/u=3686273500,1950045651&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1672419600&t=40dcde0e36cec07218f8f480555e7dcc"
                this.location.locationUrl = this.imageUrl
                this.$message({
                  message: "添加成功",
                  type: "success",
                });
            }
        } else {
            if ( this.isSave ) {
                this.$message({
                  message: "保存失败",
                  type: "error",
                });
            } else {
                this.location = {}
                this.$message({
                  message: "添加失败",
                  type: "error",
                });
            }
        }
      })
    },
  },
};
</script>

<style>
.el-select .el-input {
    width: 130px;
  }
  .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }
</style>